package com.xoa.controller.inv;

import com.xoa.dao.crm.SupplierMapper;
import com.xoa.dao.inv.PoCommodityEnterMapper;
import com.xoa.model.crm.Supplier;
import com.xoa.model.inv.OmShipment;
import com.xoa.model.inv.PoCommodityEnter;
import com.xoa.model.inv.Repayment;
import com.xoa.service.inv.PoCommodityEnterService;
import com.xoa.util.ToJson;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 张丽军 商品入库管理
 */
@Controller
@RequestMapping("/poCommodityEnter")
public class PoCommodityEnterController {

    @Resource
    private PoCommodityEnterService poCommodityEnterService;
    @Resource
    private PoCommodityEnterMapper poCommodityEnterMapper;
    @Resource
    private SupplierMapper supplierMapper;
    //处理时间  将前台传过来的时间存到数据库中
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    //   商品入库管理的页面
    @RequestMapping("/purchaSestock")
    public String  index(HttpServletRequest request){
        return "app/storage/purchaseManage/purchaSestock";
    }

    //   商品出库管理的页面的映射
    @RequestMapping("/purchaseOut")
    public String  purchaseOut(HttpServletRequest request){
        return "app/storage/purchaseManage/purchaseReturns/purchaseOut";
    }

    //  采购进货查询页面的映射
    @RequestMapping("/arrearsStatistics")
    public String  arrearsStatistics(HttpServletRequest request){
        return "app/storage/purchaseManage/arrearsStatistics";
    }

    //  还供货商款页面的映射
    @RequestMapping("/newRepayment")
    public String  newRepayment(HttpServletRequest request){
        return "app/storage/purchaseManage/purchaseReturns/newRepayment";
    }


    /**
     * 新建商品信息接口
     * @param request
     * @param poCommodityEnter
     * @return
     */
    @ResponseBody
    @RequestMapping("/insertProduct")
    public ToJson<PoCommodityEnter> insertProduct(HttpServletRequest request, PoCommodityEnter poCommodityEnter){
        return poCommodityEnterService.insertProduct(request,poCommodityEnter);
    }

    /**
     * 修改商品信息接口
     * @param request
     * @param poCommodityEnter
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateProduct")
    public ToJson<PoCommodityEnter> updateProduct(HttpServletRequest request,PoCommodityEnter poCommodityEnter){
        return poCommodityEnterService.updateProduct(request,poCommodityEnter);
    }

    /**
     * 查询商品详情信息
     * @param request
     * @param enterId
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectProductById")
    public ToJson<PoCommodityEnter> selectProductById(HttpServletRequest request,Integer enterId){
        return poCommodityEnterService.selectProductById(request,enterId);
    }

    /**
     * 删除商品信息
     * @param request
     * @param enterId
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteProductById")
    public ToJson<PoCommodityEnter> deleteProductById(HttpServletRequest request,Integer enterId){
        return poCommodityEnterService.deleteProductById(request,enterId);
    }

    /**
     * 条件查询仓库信息
     * @param request
     * @param poCommodityEnter
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectProductInfo")
    public ToJson<PoCommodityEnter> selectProductInfo(HttpServletRequest request,PoCommodityEnter poCommodityEnter,Integer type,Integer isArrears,Integer page,Integer limit){
        return poCommodityEnterService.selectProductInfo(request,poCommodityEnter,type,isArrears,page,limit);
    }
    /**
     * 查询所有出货人
     */
    @RequestMapping("/selectUsers")
    @ResponseBody
    public ToJson<PoCommodityEnter> selectUsers() {
        return poCommodityEnterService.selectUsers();
    }
    /**
     * 查询欠款的供应商
     */
    @RequestMapping("/selectSupplier")
    @ResponseBody
    public ToJson<PoCommodityEnter> selectSupplier(String supplierName) {
        ToJson json = new ToJson<PoCommodityEnter>(1, "err");
        try {
            List<PoCommodityEnter> poCommodityEnters = poCommodityEnterService.selectSupplier(supplierName);
            List<PoCommodityEnter> news = new ArrayList<>();
            if (supplierName != null && supplierName != "") {
                for (PoCommodityEnter poCommodityEnter : poCommodityEnters
                ) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("obj", poCommodityEnter);
                    map.put("isArrears", 1);
                    List<PoCommodityEnter> info = poCommodityEnterMapper.selectProductInfo(map);
                    for (PoCommodityEnter poCommodityEnter1 : info
                    ) {
                        //供应商名称
                        if(poCommodityEnter1.getSupplierId()!=0&&poCommodityEnter1.getSupplierId()!=null){
                            Supplier supplier = supplierMapper.selectByPrimaryKey(poCommodityEnter1.getSupplierId());
                            if(supplier!=null){
                                poCommodityEnter1.setSupplierName(supplier.getSupplierName());
                            }
                        }
                        news.add(poCommodityEnter1);
                    }
                }
                json.setObj(news);
                json.setFlag(0);
                json.setMsg("true");
            } else {
                json.setObj(poCommodityEnters);
                json.setFlag(0);
                json.setMsg("true");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 商品库存
     */
    @RequestMapping("/CommodityStocks")
    @ResponseBody
    public ToJson seleceStock(HttpServletRequest request,PoCommodityEnter poCommodityEnter,Integer page,Integer limit){
        return poCommodityEnterService.seleceStock(request,poCommodityEnter,page,limit);
    }

}
